
import sys

import pygame
from sprites import *
import random
from tkinter import messagebox



screen = pygame.display.set_mode(Settings.SCREEN_RECT.size)
clock = pygame.time.Clock()
game_still = True
player = None
player2 = None
enemies = None
props1 = None  # 加速道具
props2 = None  # 补血道具
enemy_bullets = None
walls = None
water = None
direction = Settings.UP  # 初始化坦克刚出现时
count = 0  # 记录杀死的敌机

player2_speed = Settings.player2_SPEED
player_speed = Settings.player_SPEED
"""
初始化游戏的一些设置
:return:
"""
pygame.init()   # 初始化pygame模块
pygame.display.set_caption(Settings.GAME_NAME)  # 设置窗口标题
pygame.mixer.init()    # 初始化音频模块

def create_sprite():
    global player
    global player2
    player = Player(Settings.player_IMAGE_NAME, screen)
    player2 = Player(Settings.player2_IMAGE_NAME, screen)
    global enemies
    enemies = pygame.sprite.Group()
    global props1
    props1 = pygame.sprite.Group()
    global props2
    props2 = pygame.sprite.Group()
    global enemy_bullets
    enemy_bullets = pygame.sprite.Group()
    global walls
    walls = pygame.sprite.Group()
    global water
    water = pygame.sprite.Group()
    for i in range(Settings.ENEMY_COUNT):
        direction = random.randint(0, 3)
        enemy = Enemy(Settings.ENEMY_IMAGES[direction], screen, out_time=random.randint(10, 15), hp=1)
        enemy.direction = direction
        enemies.add(enemy)
    for i in range(Settings.ENEMY_COUNT2):
        direction = random.randint(0, 3)
        enemy = Enemy(Settings.ENEMY_IMAGES2[direction], screen, out_time=random.randint(10, 25), hp=2)
        enemy.direction = direction
        enemies.add(enemy)
    for i in range(Settings.ENEMY_COUNT3):
        direction = random.randint(0, 3)
        enemy = Enemy(Settings.ENEMY_IMAGES3[direction], screen, out_time=random.randint(15, 25), hp=3)
        enemy.direction = direction
        enemies.add(enemy)

    draw_map()


"""
绘制地图
:return:
"""
def draw_map():
    for y in range(len(Settings.MAP_ONE)):
        for x in range(len(Settings.MAP_ONE[y])):
            if Settings.MAP_ONE[y][x] == 0:
                continue
            wall = Wall(Settings.WALLS[Settings.MAP_ONE[y][x]], screen)
            wall.rect.x = x*Settings.BOX_SIZE
            wall.rect.y = y*Settings.BOX_SIZE
            if Settings.MAP_ONE[y][x] == Settings.RED_WALL:
                wall.type = Settings.RED_WALL
            elif Settings.MAP_ONE[y][x] == Settings.IRON_WALL:
                wall.type = Settings.IRON_WALL
            elif Settings.MAP_ONE[y][x] == Settings.WEED_WALL:
                wall.type = Settings.WEED_WALL
            elif Settings.MAP_ONE[y][x] == Settings.WATER_WALL:
                wall.type = Settings.WATER_WALL
            elif Settings.MAP_ONE[y][x] == Settings.home_WALL:
                wall.type = Settings.home_WALL
                wall.life = 1
            walls.add(wall)

def check_keydown(event):
    """检查按下按钮的事件"""
    if event.key == pygame.K_LEFT:
        # 按下左键
        player.direction = Settings.LEFT
        player.is_moving = True
        player.is_hit_wall = False
    elif event.key == pygame.K_RIGHT:
        # 按下右键
        player.direction = Settings.RIGHT
        player.is_moving = True
        player.is_hit_wall = False
    elif event.key == pygame.K_UP:
        # 按下上键
        player.direction = Settings.UP
        player.is_moving = True
        player.is_hit_wall = False
    elif event.key == pygame.K_DOWN:
        # 按下下键
        player.direction = Settings.DOWN
        player.is_moving = True
        player.is_hit_wall = False
    elif event.key == pygame.K_m:
        # 坦克发子弹
        player.shot()

def check_keyup(event):

    """检查松开按钮的事件"""
    if event.key == pygame.K_LEFT:
        # 松开左键
        player.direction = Settings.LEFT
        player.is_moving = False
    elif event.key == pygame.K_RIGHT:
        # 松开右键
        player.direction = Settings.RIGHT
        player.is_moving = False
    elif event.key == pygame.K_UP:
        # 松开上键
        player.direction = Settings.UP
        player.is_moving = False
    elif event.key == pygame.K_DOWN:
        # 松开下键
        player.direction = Settings.DOWN
        player.is_moving = False


def check_keydown2(event):

    """检查按下按钮的事件"""
    global direction
    if event.key == pygame.K_a:
        # 按下左键
        direction = Settings.LEFT
        player2.direction = Settings.LEFT
        player2.is_moving = True
        player2.is_hit_wall = False
    elif event.key == pygame.K_d:
        # 按下右键
        direction = Settings.RIGHT
        player2.direction = Settings.RIGHT
        player2.is_moving = True
        player2.is_hit_wall = False
    elif event.key == pygame.K_w:
        # 按下上键
        direction = Settings.UP
        player2.direction = Settings.UP
        player2.is_moving = True
        player2.is_hit_wall = False
    elif event.key == pygame.K_s:
        # 按下下键
        direction = Settings.DOWN
        player2.direction = Settings.DOWN
        player2.is_moving = True
        player2.is_hit_wall = False
    elif event.key == pygame.K_SPACE:
        # 坦克发子弹
        player2.shot()

def check_keyup2(event):
    """检查松开按钮的事件"""
    global direction
    if event.key == pygame.K_a:
        # 松开左键
        direction = Settings.LEFT
        player2.direction = Settings.LEFT
        player2.is_moving = False
    elif event.key == pygame.K_d:
        # 松开右键
        direction = Settings.RIGHT
        player2.direction = Settings.RIGHT
        player2.is_moving = False
    elif event.key == pygame.K_w:
        # 松开上键
        direction = Settings.UP
        player2.direction = Settings.UP
        player2.is_moving = False
    elif event.key == pygame.K_s:
        # 松开下键
        direction = Settings.DOWN
        player2.direction = Settings.DOWN
        player2.is_moving = False
def event_handler():
    for event in pygame.event.get():
        # 判断是否是退出游戏
        if event.type == pygame.QUIT:
            game_over()
        elif event.type == pygame.KEYDOWN:
            if player.is_alive:
                check_keydown(event)
            if player2.is_alive:
                check_keydown2(event)
        elif event.type == pygame.KEYUP:
            if player.is_alive:
                check_keyup(event)
            if player2.is_alive:
                check_keyup2(event)
# 碰撞逻辑
def check_collide():
    global game_still, player2_speed , player_speed

    # 保证坦克不移出屏幕
    player.hit_wall()
    player2.hit_wall()
    for enemy in enemies:
        enemy.hit_wall_turn()

    # 子弹击中墙
    for wall in walls:
        # 我方英雄子弹击中墙
        for bullet in player.bullets:
            if pygame.sprite.collide_rect(wall, bullet):
                if wall.type == Settings.RED_WALL:
                    wall.kill()
                    bullet.kill()
                    if random.randint(1, 10) == 2:
                        prop_2 = Prop(image_name=Settings.PROP2, screen=screen, proRect=wall.rect)
                        props2.add(prop_2)
                elif wall.type == Settings.home_WALL:
                    game_still = False
                elif wall.type == Settings.IRON_WALL:
                    bullet.kill()
        for bullet in player2.bullets:
            if pygame.sprite.collide_rect(wall, bullet):
                if wall.type == Settings.RED_WALL:
                    wall.kill()
                    bullet.kill()
                    if random.randint(1, 10) == 2:
                        prop_2 = Prop(image_name=Settings.PROP2, screen=screen, proRect=wall.rect)
                        props2.add(prop_2)
                elif wall.type == Settings.home_WALL:
                    game_still = False
                elif wall.type == Settings.IRON_WALL:
                    bullet.kill()
        # 敌方英雄子弹击中墙
        for enemy in enemies:
            for bullet in enemy.bullets:
                if pygame.sprite.collide_rect(wall, bullet):
                    if wall.type == Settings.RED_WALL:
                        wall.kill()
                        bullet.kill()
                    elif wall.type == Settings.home_WALL:
                        game_still = False
                    elif wall.type == Settings.IRON_WALL:
                        bullet.kill()

        # 我方坦克撞墙
        if pygame.sprite.collide_rect(player, wall):
            # 不可穿越墙
            if wall.type == Settings.RED_WALL or wall.type == Settings.IRON_WALL or wall.type == Settings.home_WALL:
                player.is_hit_wall = True
                # 移出墙内
                player.move_out_wall(wall)
            elif wall.type == Settings.WATER_WALL:
                player.is_hit_wall = False
                t0 = clock.get_time()
                player.water(t0)
        if player.rect.right > wall.rect.left and player.rect.bottom > wall.rect.top and player.rect.left > wall.rect.right and player.rect.top > wall.rect.bottom:
            player.water(2000)

        if pygame.sprite.collide_rect(player2, wall):
            # 不可穿越墙
            if wall.type == Settings.RED_WALL or wall.type == Settings.IRON_WALL or wall.type == Settings.home_WALL:
                player2.is_hit_wall = True
                # 移出墙内
                player2.move_out_wall(wall)
            elif wall.type == Settings.WATER_WALL:
                player2.is_hit_wall = False
                t0 = clock.get_time()
                # player2.water_time = 0
                player2.water(t0)
        if player2.rect.right > wall.rect.left and player2.rect.bottom > wall.rect.top and player2.rect.left > wall.rect.right and player2.rect.top > wall.rect.bottom:
            player2.water(2000)

        # 敌方坦克撞墙
        for enemy in enemies:
            if pygame.sprite.collide_rect(wall, enemy):
                if wall.type == Settings.RED_WALL or wall.type == Settings.IRON_WALL or wall.type == Settings.home_WALL:
                    enemy.move_out_wall(wall)
                    enemy.random_turn()

        # player.water(2000)
        # player2.water(2000)

    for enemy in enemies:
        # 我方英雄子弹击中对方
        global count
        if enemy.is_elive:
            for bullet in player.bullets:
                if pygame.sprite.collide_rect(enemy, bullet):
                    enemy.kill()
                    bullet.kill()
                    # 杀死敌机 随机出现一个加速道具
                    # count += 1
                    if enemy.is_elive:
                        if random.randint(1, 4) == 2:
                            prop_1 = Prop(image_name=Settings.PROP, screen=screen, proRect=enemy.rect)
                            props1.add(prop_1)


            for bullet in player2.bullets:
                if pygame.sprite.collide_rect(enemy, bullet):
                    enemy.kill()
                    bullet.kill()
                    # 杀死敌机 随机出现一个加速道具
                    # count += 1
                    if enemy.is_elive:
                        if random.randint(1, 4) == 2:
                            prop_1 = Prop(image_name=Settings.PROP, screen=screen, proRect=enemy.rect)
                            props1.add(prop_1)


            # 敌我坦克碰撞
            if player.is_alive:
                if pygame.sprite.collide_rect(player, enemy):
                    # player.kill()
                    enemy.kill()
            if player2.is_alive:
                if pygame.sprite.collide_rect(player2, enemy):
                    # player2.kill2()
                    enemy.kill()
    #碰撞道具
    for prop in props1:
        if pygame.sprite.collide_rect(player, prop):
            prop.kill()
            player.speed += 1
            player.temp = player.speed
            # print("i am temp"+ str(player.temp))
        if pygame.sprite.collide_rect(player2, prop):
            prop.kill()
            player2.speed += 1
            player2.temp = player2.speed
            # print("i am temp" + str(player2.temp))
    for prop in props2:
        if pygame.sprite.collide_rect(player, prop):
            prop.kill()
            player.life = 2
        if pygame.sprite.collide_rect(player2, prop):
            prop.kill()
            player2.life = 2

    # 敌方子弹击中我方
    if player.is_alive:
        for enemy in enemies:
            for bullet in enemy.bullets:
                if bullet.rect.colliderect(player. rect):
                    bullet.kill()
                    player.kill()
    if player2.is_alive:
        for enemy in enemies:
            for bullet in enemy.bullets:
                if bullet.rect.colliderect(player2.rect):
                    bullet.kill()
                    player2.kill2()

def update_sprites():
    global direction
    t = clock.get_time()
    if player.is_moving:
        player.update()
    if player2.is_moving:
        player2.update2()
    walls.update()
    player.bullets.update()
    player2.bullets.update()
    enemies.update(t)
    # props1.update()

    for enemy in enemies:
        enemy.bullets.update()
        enemy.bullets.draw(screen)
        # enemy.draw_to(screen)
    for prop in props1:
        prop.update()
        prop.draw_to()
    for prop in props2:
        prop.update()
        prop.draw_to2()
        # screen.blit(prop.image, prop.rect)
    # enemies.draw_to(screen)
    # props1.draw(screen)
    player.bullets.draw(screen)
    player2.bullets.draw(screen)
    screen.blit(player.image, player.rect)
    screen.blit(player2.image, player2.rect)
    walls.draw(screen)
    text = pygame.font.Font('C:\\Windows\\Fonts\\simhei.ttf', 20)
    text_fmt = text.render('剩余敌机数量 :' + str(len(enemies)), True, pygame.Color(0,255,255),None)
    screen.blit(text_fmt, (750, 0))
def main():
    create_sprite()
    player2.rect.centerx = Settings.SCREEN_RECT.centerx - Settings.BOX_RECT.width * 2
    player2.rect.bottom = Settings.SCREEN_RECT.bottom
    while True and (player.is_alive or player2.is_alive) and game_still and len(enemies) != 0:
        screen.fill(Settings.SCREEN_COLOR)
        # 1、设置刷新帧率
        clock.tick(Settings.FPS)
        # 2、事件监听
        event_handler()
        # 3、碰撞监测
        check_collide()
        # 4、更新/绘制精灵/经理组
        update_sprites()
        # 5、更新显示
        pygame.display.update()

    game_over()

# @staticmethod
def game_over():
    if len(enemies) == 0:
        messagebox.showwarning("提示", "u Win")
    else:
        messagebox.showwarning("提示", "游戏结束")
    pygame.quit()
    sys.exit()

if __name__ == '__main__':
    main()
